﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Linq.Expressions;
using System.Web.UI.WebControls;
using System.Collections;
using cn.Util;
using yq.Enums;

namespace System.Web.Mvc
{
    /// <summary>
    /// 扩展
    /// </summary>
    public static class HtmlExtensions
    {

      


        #region 列表框 单选框 双值框


        /// <summary>
        /// 双值框
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="link">显示内容，可以是控件ID 也可以是链接地址，如果是控件必须已#开始</param>
        /// <param name="val">默认显示的值</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString DoubleDialog<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string link, string val, object htmlAttributes = null)
        {
            var metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);

            string name = ExpressionHelper.GetExpressionText(expression);
            string id=name.Replace(".","_");
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            IDictionary<string, object> attributes = GetHtmlAttributes(htmlAttributes);


            //设置显示的按钮
            TagBuilder tagBuilder = new TagBuilder("input");
            tagBuilder.MergeAttribute("id", "val_" + id);
            tagBuilder.MergeAttribute("onclick", "$.DoubleDialog('" + link + "','" + "#" + id + "','#val_" + id + "');");
            SetHtmlAttributes(ref attributes, "class", "btn btn-mini");
            tagBuilder.MergeAttributes<string, object>(attributes);
            tagBuilder.MergeAttribute("value", string.IsNullOrEmpty(val) ? " [选择] " : val.ToString());
            tagBuilder.MergeAttribute("type", "button");

            //隐藏域 实际值
            object key = htmlHelper.ViewData.Eval(fullHtmlFieldName);
            TagBuilder taghidden = new TagBuilder("input");
            taghidden.MergeAttribute("type", "hidden");
            taghidden.MergeAttribute("id", id);
            taghidden.MergeAttribute("name", name);
            taghidden.MergeAttribute("value",key==null?"":key.ToString());

            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing) + taghidden.ToString(TagRenderMode.SelfClosing));
        }



        /// <summary>
        /// 下拉列表框 只能用在enum
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="delEnum">排除这个枚举</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString SelectEnumFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
             Expression<Func<TModel, TProperty>> expression, Enum delEnum = null,
            object htmlAttributes = null)
        {
            var metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);

            string name = ExpressionHelper.GetExpressionText(expression);
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            //是否有值
            Enum modelValue = (Enum)htmlHelper.ViewData.Eval(fullHtmlFieldName);

            IList<SelectListItem> selectitem = new List<SelectListItem>();

            //循环创建复选框
            foreach (var item in modelValue.GetDictionary())
            {
                if (delEnum == null || delEnum.GetHashCode() != item.Key)
                {
                    selectitem.Add(new SelectListItem()
                    {
                        Selected = modelValue.GetHashCode() == item.Key,
                        Text = item.Value,
                        Value = item.Key.ToString()
                    });
                }
            }

            selectitem = selectitem.OrderByDescending(obj => obj.Selected).ToList();

            return Html.SelectExtensions.DropDownList(htmlHelper, name, selectitem, htmlAttributes);
        }

        /// <summary>
        /// 单选框组,增加了labfor的文本显示功能 只能用在enum
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="direction"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString RadioEnumFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            bool IsHorizontal = true,
            object htmlAttributes = null)
        {
            IDictionary<string, object> attributes = GetHtmlAttributes();

            //从字段推导出名称
            string name = ExpressionHelper.GetExpressionText(expression);
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            //是否有值
            Enum modelValue = (Enum)htmlHelper.ViewData.Eval(fullHtmlFieldName);

            TagBuilder tag = new TagBuilder("div");
            tag.MergeAttribute("css", "controls");
            StringBuilder sb = new StringBuilder();

            //循环创建复选框
            foreach (var item in modelValue.GetDictionary())
            {
                TagBuilder checkTag = new TagBuilder("input");

                checkTag.MergeAttribute("name", name);
                checkTag.MergeAttribute("type", "radio");
                checkTag.MergeAttribute("value", item.Key.ToString());
                checkTag.MergeAttributes<string, object>(attributes);

                //是否有选择的值
                if (modelValue != null && modelValue.GetHashCode() == item.Key)
                {
                    checkTag.MergeAttribute("checked", "checked");
                }


                //生成labfor
                TagBuilder labelTag = new TagBuilder("label");
                labelTag.MergeAttribute("css", IsHorizontal ? "radio inline" : "radio");

                labelTag.InnerHtml = checkTag.ToString() + " " + item.Value;

                sb.AppendLine(labelTag.ToString());
            }

            //将label添加到span中
            tag.InnerHtml = sb.ToString();

            return MvcHtmlString.Create("<div class='control-group'>" + tag.ToString() + " </div>");
        }

        #endregion


        #region 是否框


        /// <summary>
        /// 是否框,直接生成labfor信息
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="expression">表达式,obj=>obj.xxx</param>
        /// <param name="htmlAttributes">附加的特性信息,new{}</param>
        /// <returns></returns>
        public static MvcHtmlString BooleanEnumFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, BooleanType>> expression, object htmlAttributes = null)
        {
            var metadata = ModelMetadata.FromLambdaExpression<TModel, BooleanType>(expression, htmlHelper.ViewData);

            //获取对象名称
            string name = metadata.PropertyName;

            //获取当前值
            BooleanType booltype = (BooleanType)htmlHelper.ViewData.Eval(name);

            StringBuilder sb = new StringBuilder();
            TagBuilder tagBuilder = new TagBuilder("input");
            tagBuilder.MergeAttribute("type", "checkbox");

            if (booltype == BooleanType.Yes)
            {
                tagBuilder.MergeAttribute("checked", "checked");
            }

            tagBuilder.MergeAttribute("forid",name);
            tagBuilder.MergeAttribute("onchange", "BooleanTypeChange(this);");
            tagBuilder.MergeAttributes<string, object>(((IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));

            //设置默认值
            var val = booltype == BooleanType.Yes ? BooleanType.Yes.GetHashCode() : BooleanType.No.GetHashCode();
            tagBuilder.MergeAttribute("value", val.ToString());


            StringBuilder sb1 = new StringBuilder();
            TagBuilder taghidden = new TagBuilder("input");
            taghidden.MergeAttribute("type", "hidden");
            taghidden.MergeAttribute("id", name);
            taghidden.MergeAttribute("name", name);
            taghidden.MergeAttributes<string, object>(((IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
            taghidden.MergeAttribute("value", val.ToString());

            //附加labfor信息
            string labFor = GetLabForText(tagBuilder.ToString(TagRenderMode.SelfClosing)+taghidden.ToString(TagRenderMode.SelfClosing),
                metadata.DisplayName);

            return MvcHtmlString.Create(labFor);

        }

        /// <summary>
        /// 是否框,直接生成labfor信息
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="expression">表达式,obj=>obj.xxx</param>
        /// <param name="htmlAttributes">附加的特性信息,new{}</param>
        /// <returns></returns>
        public static MvcHtmlString BooleanFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, object htmlAttributes = null)
        {
            //使用原有的构造
            MvcHtmlString info = Html.InputExtensions.CheckBoxFor(htmlHelper, expression, htmlAttributes);

            var metadata = ModelMetadata.FromLambdaExpression<TModel, bool>(expression, htmlHelper.ViewData);
            //获取对象名称
            string name = metadata.PropertyName;


            //附加labfor信息
            string labFor = GetLabForText(info.ToString(), metadata.DisplayName);

            info = MvcHtmlString.Create(labFor);

            return info;
        }
        /// <summary>
        /// 返回LabFor的文本信息
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="displayName"></param>
        /// <returns></returns>
        private static string GetLabForText(string inputString, string displayName)
        {
            return "<label  >" + inputString + " " + (displayName ?? "NameIsNull") + "</label>";
        }

        #endregion


        /// <summary>
        /// 根据模型的属性设置直接生成文本标题
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="html"></param>
        /// <param name="expression">类似:obj=>obj.属性名</param>
        /// <param name="isShort">是否显示短名称,默认为否,显示长名称</param>
        /// <returns></returns>
        public static MvcHtmlString RawFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, bool isShort = false)
        {
            var metadata = ModelMetadata.FromLambdaExpression<TModel, TValue>(expression, html.ViewData);
            var htmlFieldName = ExpressionHelper.GetExpressionText(expression);

            string str = "";
            if (isShort == false)
            {
                str = metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last<string>());
            }
            else
            {
                str = metadata.ShortDisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last<string>());
            }

            if (string.IsNullOrEmpty(str))
            {
                return MvcHtmlString.Empty;
            }
            else
            {
                return MvcHtmlString.Create(str);
            }
        }



        ///// <summary>
        ///// 显示链接并判断权限
        ///// </summary>
        ///// <param name="htmlHelper"></param>
        ///// <param name="linkText"></param>
        ///// <param name="actionName"></param>
        ///// <param name="controllerName"></param>
        ///// <param name="areaName"></param>
        ///// <param name="routeValues"></param>
        ///// <param name="htmlAttributes"></param>
        ///// <returns></returns>
        //public static MvcHtmlString ActionRoleLink(this HtmlHelper htmlHelper, RoleLinkButton info,
        //    object routeValues = null,
        //    object htmlAttributes = null)
        //{
        //    MvcHtmlString mvcString = null;
        //    mvcString = ActionRoleLink(htmlHelper, info.LinkText, info.ActionName, info.ControllerName, info.AreaName, routeValues, htmlAttributes);
        //    info.IsShowButton = mvcString == null ? BooleanType.No : BooleanType.Yes;

        //    return mvcString;
        //}

        ///// <summary>
        ///// 显示链接并判断权限
        ///// </summary>
        ///// <param name="htmlHelper"></param>
        ///// <param name="linkText"></param>
        ///// <param name="actionName"></param>
        ///// <param name="controllerName"></param>
        ///// <param name="areaName"></param>
        ///// <param name="routeValues"></param>
        ///// <param name="htmlAttributes"></param>
        ///// <returns></returns>
        //public static MvcHtmlString ActionRoleLink(this HtmlHelper htmlHelper, string linkText,
        //    string actionName,
        //    string controllerName,
        //    string areaName = null,
        //    object routeValues = null,
        //    object htmlAttributes = null, yq.Enums.BooleanType type = yq.Enums.BooleanType.UnKnow)
        //{
        //    bool isShowButton = false;
        //    MvcHtmlString mvcString = null;

        //    //权限页面
        //    PurviewPage page = new PurviewPage();

        //    if (type == yq.Enums.BooleanType.UnKnow)
        //    {
        //        isShowButton = yq.ControllerBase.BaseController.IsPurview(actionName, controllerName, areaName, out page);
        //    }

        //    //如果允许操作
        //    if (isShowButton || type == yq.Enums.BooleanType.Yes)
        //    {
        //        //添加路由
        //        var route = AddRouteValuesArea(routeValues, areaName);
        //        var htmlLists = GetHtmlAttributes(htmlAttributes);

        //        mvcString = System.Web.Mvc.Html.LinkExtensions.ActionLink(htmlHelper,
        //                        linkText,
        //                        actionName,
        //                        controllerName,
        //                        route,
        //                        htmlLists);
        //    }

        //    return mvcString;
        //}

        /// <summary>
        /// 返回图片标签
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="type"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString Img(this HtmlHelper htmlHelper, string url, object htmlAttributes = null)
        {
            UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
            string imgSrc = UrlExtensions.GetUrlString(urlHelper, "{0}", url);
            StringBuilder sb = new StringBuilder();
            TagBuilder tagBuilder = new TagBuilder("img");
            tagBuilder.MergeAttribute("src", imgSrc);
            tagBuilder.MergeAttributes<string, object>(((IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
        }

        ///// <summary>
        ///// 显示图片
        ///// </summary>
        ///// <param name="htmlHelper"></param>
        ///// <param name="id"></param>
        ///// <param name="htmlAttributes"></param>
        ///// <returns></returns>
        //public static MvcHtmlString ImgFile(this HtmlHelper htmlHelper, string id, int maxWidth = 0, int maxHeight = 0, int quality = -1, object htmlAttributes = null)
        //{
        //    const string tp = "~/home/PublicReuse/ShowImg?id={0}&maxWidth={1}&maxHeight={2}&quality={3}";
        //    UrlHelper Url = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
        //    IDictionary<string, object> attributes = HtmlExtensions.GetHtmlAttributes(htmlAttributes);

        //    var info = yq.Model.Logic.SysFile.Load(id);
        //    TagBuilder builder = new TagBuilder("img");

        //    if (info.IsLoad)
        //    {
        //        builder.MergeAttribute("src", Url.Content(string.Format(tp, id, maxWidth, maxHeight, quality)), true);
        //        builder.MergeAttribute("alt", info.FlieName, true);
        //        builder.MergeAttributes<string, object>(attributes);

        //        return MvcHtmlString.Create(builder.ToString());
        //    }
        //    else
        //    {
        //        return MvcHtmlString.Create("");
        //    }
        //}




        #region 按钮



        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="text"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString Submit(this HtmlHelper htmlHelper, string text = "提 交", object htmlAttributes = null)
        {
            return Button(htmlHelper, "btn btn-primary", "submit", text, htmlAttributes);
        }

        /// <summary>
        /// 重置按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="text"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString Reset(this HtmlHelper htmlHelper, string text = "重 置", object htmlAttributes = null)
        {
            return Button(htmlHelper, "btn", "reset", text, htmlAttributes);
        }

        /// <summary>
        /// 普通按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="text"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper htmlHelper, string text, object htmlAttributes = null)
        {
            return Button(htmlHelper, "btn btn-primary", "button", text, htmlAttributes);
        }

        /// <summary>
        /// 生成按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="className"></param>
        /// <param name="type"></param>
        /// <param name="text"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        private static MvcHtmlString Button(HtmlHelper htmlHelper, string className, string type, string text, object htmlAttributes = null)
        {
            UrlHelper urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
            StringBuilder sb = new StringBuilder();
            TagBuilder tagBuilder = new TagBuilder("button");

            IDictionary<string, object> dic = GetHtmlAttributes(htmlAttributes);

            if (dic.ContainsKey("class") == false)
            {
                tagBuilder.MergeAttribute("class", className);
            }

            tagBuilder.InnerHtml = text;
            tagBuilder.MergeAttributes<string, object>(((IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
            return MvcHtmlString.Create(tagBuilder.ToString().Replace("&#39;", "'"));
        }
        #endregion


        #region 时间控件


        /// <summary>
        /// 时间控件
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="className"></param>
        /// <param name="type"></param>
        /// <param name="text"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString TimeFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, System.Linq.Expressions.Expression<Func<TModel, TProperty>> expression, DatePickerFmt dateFmt, object htmlAttributes = null)
        {
            IDictionary<string, object> attributes = GetHtmlAttributes();
            string name = ExpressionHelper.GetExpressionText(expression);
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            string optionsStr = "";
            switch (dateFmt)
            {
                case DatePickerFmt.yyyyMMddHHmmss:
                    optionsStr = ("yyyy-MM-dd HH:mm:ss");
                    break;
                case DatePickerFmt.HHmmss:
                    optionsStr = ("HH:mm:ss");
                    break;
                case DatePickerFmt.yyyyMM:
                    optionsStr = ("yyyy-MM");
                    break;
                case DatePickerFmt.yyyyMMddHHmm:
                    optionsStr = ("yyyy-MM-dd HH:mm");
                    break;
                case DatePickerFmt.yyyy:
                    optionsStr = ("yyyy");
                    break;
                case DatePickerFmt.yyyyMMdd:
                    optionsStr = ("yyyy-MM-dd");
                    break;
            }

            TagBuilder builder = new TagBuilder("input");

            builder.MergeAttribute("type", "text", true);
            builder.MergeAttribute("id", name, true);
            builder.MergeAttribute("name", name, true);
            builder.MergeAttribute("onclick", string.Format("WdatePicker({0});", "{dateFmt:'" + optionsStr + "'}"), true);
            SetHtmlAttributes(ref attributes, "style", "width:190px");
            string text = ConvertToString(Convert.ToString(htmlHelper.ViewData.Eval(fullHtmlFieldName), System.Globalization.CultureInfo.CurrentCulture), optionsStr);
            builder.MergeAttribute("value", text, true);

            ModelState modelState;
            if (htmlHelper.ViewData.ModelState.TryGetValue(fullHtmlFieldName, out modelState) && modelState.Errors.Count > 0)
            {
                builder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
            }

            ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
            
            
            //builder.MergeAttributes<string, object>(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));

            ///const string span = "<span  class='add-on'><i class='icon-time'></i></span>";

            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }

        /// <summary>
        /// 日期格式化
        /// </summary> 
        public enum DatePickerFmt
        {
            /// <summary>
            /// yyyy-MM-dd HH:mm:ss
            /// 2008-03-12 19:20:00
            /// </summary>
            yyyyMMddHHmmss,
            /// <summary>
            /// yyyy-MM-dd
            /// 2008-03-12
            /// </summary>
            yyyyMMdd,
            /// <summary>
            /// HH:mm:ss
            /// 12:04:00
            /// </summary>
            HHmmss,
            /// <summary>
            /// yyyyMM
            /// 2012-03
            /// </summary>
            yyyyMM,

            /// <summary>
            /// yyyyMMddHHmm
            /// 2008-03-12 19:20
            /// </summary>
            yyyyMMddHHmm,

            /// <summary>
            /// yyyy
            /// 2012
            /// </summary>
            yyyy
        }

        /// <summary>
        /// 时间转换
        /// </summary>
        /// <param name="time"></param>
        /// <param name="fmt"></param>
        /// <returns></returns>
        internal static string ConvertToString(object time, string fmt)
        {
            string str = null;
            DateTime dt = DateTime.MinValue;
            if (time != null && DateTime.TryParse(time.ToString(), out dt) == true)
            {
                str = dt.ToString(fmt);
            }

            return str;
        }

        #endregion


        #region 内部方法


        /// <summary>
        /// 获取运行值
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="viewData"></param>
        /// <param name="key"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        internal static object GetModelStateValue<TModel>(ViewDataDictionary<TModel> viewData, string key, Type destinationType)
        {
            ModelState modelState;
            if (viewData.ModelState.TryGetValue(key, out modelState) && modelState.Value != null)
            {
                return modelState.Value.ConvertTo(destinationType, null);
            }

            return null;
        }




        /// <summary>
        /// 检测是否存在指定样式
        /// </summary>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static IDictionary<string, object> GetHtmlAttributes(object htmlAttributes = null)
        {
            IDictionary<string, object> obj = null;
            if (htmlAttributes == null)
            {
                obj = new Dictionary<string, object>();
            }
            else
            {
                obj = ((IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            }

            return obj;
        }

        /// <summary>
        /// 检测是否存在指定样式,如果没有则添加
        /// </summary>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static IDictionary<string, object> SetHtmlAttributes(ref IDictionary<string, object> attributes, string key, string val)
        {
            if (!attributes.ContainsKey(key))
            {
                attributes.Add(key, val);
            }

            return attributes;
        }


        /// <summary>
        /// 为路由添加区域信息
        /// </summary>
        /// <param name="routeValues"></param>
        /// <param name="area"></param>
        private static RouteValueDictionary AddRouteValuesArea(object routeValues, string area)
        {
            RouteValueDictionary obj = null;
            if (routeValues == null)
            {
                obj = new RouteValueDictionary();
            }
            else
            {
                obj = new RouteValueDictionary(routeValues);
            }

            if (string.IsNullOrEmpty(area) == false)
            {
                if (obj.ContainsKey("area") == false)
                {
                    obj.Add("area", area);
                }
            }

            return obj;
        }

        #endregion

    }

 

}
